package com.mspbots.core.wise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mspbots.core.AbstractBaseServiceImpl;
import com.mspbots.core.wise.mapper.TriggerEscalationMapper;
import com.mspbots.core.wise.model.TenantRule;
import com.mspbots.core.wise.model.TriggerEscalation;
import com.mspbots.core.wise.service.TenantRuleService;
import com.mspbots.core.wise.service.TriggerEscalationService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TriggerEscalationServiceImpl extends AbstractBaseServiceImpl<TriggerEscalationMapper, TriggerEscalation> implements TriggerEscalationService {

    @Override
    public List<TriggerEscalation> selectByIds(Long tenantId, Long ruleId) {
        QueryWrapper<TriggerEscalation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", tenantId);
        queryWrapper.eq("rule_id", ruleId);
        queryWrapper.orderByAsc("id");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean defaultTemplate(Long tenantId, Long ruleId, TenantRuleService tenantRuleService) {
        if (ruleId == 0) {
            Map<String, Object> param = new HashMap<>();
            param.put("tenant_id", tenantId);
            removeByMap(param);
            List<TenantRule> tenantRules = tenantRuleService.selectByTenantId(tenantId);
            List<TriggerEscalation> triggerEscalations = selectByIds(tenantId, ruleId);
            for (TenantRule tenantRule : tenantRules) {
                for (int i = 0; i < 3; i++) {
                    TriggerEscalation triggerEscalation = new TriggerEscalation();
                    triggerEscalation.setTenantId(tenantRule.getTenantId());
                    triggerEscalation.setRuleId(tenantRule.getRuleId());
                    if (i == 0) {
                        triggerEscalation.setLevel((long) 1);
                        triggerEscalation.setTimes((long) 3);
                        triggerEscalation.setTemplate("Dear {USER}, you have triggered {TRIGGERNAME} {TIMES} times this week. " +
                                "If you need help to fix this issue, please reach out to {MANAGER}. The system will notify {MANAGER} after {NEXTIMES} times.");
                    } else if (i == 1) {
                        triggerEscalation.setLevel((long) 2);
                        triggerEscalation.setTimes((long) 6);
                        triggerEscalation.setTemplate("Dear {MANAGER}, {USER} has triggered {TRIGGERNAME} {TIMES} times this week. " +
                                "Please reach out to {USER} to help fix the issue. The system will notify {NEXTMANAGER} after {NEXTIMES} times.");
                    } else {
                        triggerEscalation.setLevel((long) 3);
                        triggerEscalation.setTimes((long) 9);
                        triggerEscalation.setTemplate("Dear {MANAGER}, {USER} has triggered {TRIGGERNAME} {TIMES} times this week. Please reach out to {USER} to help fix the issue.");
                    }
                    triggerEscalations.add(triggerEscalation);
                }
            }
            saveBatch(triggerEscalations);
        } else {
            Map<String, Object> param = new HashMap<>();
            param.put("tenant_id", tenantId);
            param.put("rule_id", ruleId);
            removeByMap(param);
            List<TriggerEscalation> triggerEscalations = selectByIds(tenantId, ruleId);
            for (int i = 0; i < 3; i++) {
                TriggerEscalation triggerEscalation = new TriggerEscalation();
                triggerEscalation.setTenantId(tenantId);
                triggerEscalation.setRuleId(ruleId);
                if (i == 0) {
                    triggerEscalation.setLevel((long) 1);
                    triggerEscalation.setTimes((long) 3);
                    triggerEscalation.setTemplate("Dear {USER}, you have triggered {TRIGGERNAME} {TIMES} times this week. " +
                            "If you need help to fix this issue, please reach out to {MANAGER} for help, system will notify {MANAGER} after {NEXTIMES} times.");
                } else if (i == 1) {
                    triggerEscalation.setLevel((long) 2);
                    triggerEscalation.setTimes((long) 6);
                    triggerEscalation.setTemplate("Dear {MANAGER}, {USER} have triggered {TRIGGERNAME} {TIMES} times this week. " +
                            "Please reach out to {USER} to help fixing the issue, system will notify {NEXTMANAGER} after {NEXTIMES} times.");
                } else {
                    triggerEscalation.setLevel((long) 3);
                    triggerEscalation.setTimes((long) 9);
                    triggerEscalation.setTemplate("Dear {MANAGER}, {USER} have triggered {TRIGGERNAME} {TIMES} times this week. Please reach out to {USER} to help fixing the issue.");
                }
                triggerEscalations.add(triggerEscalation);
            }
            saveBatch(triggerEscalations);
        }
        return true;
    }

}
